<template>
  <d2-container>
    <template slot="header">
      <el-form :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item label="租户姓名">
          <el-input v-model="searchForm.tenantName" placeholder="请输入租户姓名"></el-input>
        </el-form-item>
        <el-form-item label="房间号">
          <el-input v-model="searchForm.roomNumber" placeholder="请输入房间号"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </template>
    
    <el-table
      :data="tableData"
      v-loading="loading"
      style="width: 100%">
      <el-table-column
        prop="tenantName"
        label="租户姓名"
        width="120">
      </el-table-column>
      <el-table-column
        prop="roomNumber"
        label="房间号"
        width="100">
      </el-table-column>
      <el-table-column
        prop="balance"
        label="账户余额"
        width="120">
        <template slot-scope="scope">
          ¥{{ scope.row.balance }}
        </template>
      </el-table-column>
      <el-table-column
        prop="lastSettlementDate"
        label="上次结算日期"
        width="180">
      </el-table-column>
      <el-table-column
        label="操作"
        width="150">
        <template slot-scope="scope">
          <el-button size="mini" type="danger" @click="handleSettlement(scope.row)">销户结算</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <template slot="footer">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pagination.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pagination.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pagination.total">
      </el-pagination>
    </template>
  </d2-container>
</template>

<script>
import faker from 'faker'

export default {
  name: 'FinanceSettlement',
  data () {
    return {
      loading: false,
      searchForm: {
        tenantName: '',
        roomNumber: ''
      },
      tableData: [],
      pagination: {
        currentPage: 1,
        pageSize: 10,
        total: 0
      }
    }
  },
  mounted () {
    this.getList()
  },
  methods: {
    getList () {
      this.loading = true
      // 模拟数据
      const data = {
        totalCount: 50,
        pageSize: 10,
        totalPage: 5,
        currPage: this.pagination.currentPage,
        list: Array.from({ length: 10 }, (_, index) => ({
          id: index + 1,
          tenantName: faker.name.findName(),
          roomNumber: `10${index + 1}`,
          balance: faker.finance.amount(0, 500, 2),
          lastSettlementDate: faker.date.past().toISOString().split('T')[0]
        }))
      }
      this.tableData = data.list
      this.pagination.total = data.totalCount
      this.loading = false
    },
    handleSearch () {
      this.pagination.currentPage = 1
      this.getList()
    },
    handleReset () {
      this.searchForm = {
        tenantName: '',
        roomNumber: ''
      }
      this.pagination.currentPage = 1
      this.getList()
    },
    handleSettlement (row) {
      this.$confirm(`确认对租户 ${row.tenantName} 进行销户结算？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message.success('销户结算成功')
        this.getList()
      }).catch(() => {
        this.$message.info('已取消销户结算')
      })
    },
    handleSizeChange (val) {
      this.pagination.pageSize = val
      this.getList()
    },
    handleCurrentChange (val) {
      this.pagination.currentPage = val
      this.getList()
    }
  }
}
</script>

<style scoped>
.demo-form-inline .el-form-item {
  margin-bottom: 10px;
}
</style>


